package com.lianniu.web.pay.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.buyerOrder.model.OrderInfoVo;
import com.lianniu.web.buyerOrder.service.IBuyerOrderService;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.pay.dao.PayDao;
import com.lianniu.web.pay.model.PayRequestDetailVo;
import com.lianniu.web.pay.model.PayRequestVo;
import com.lianniu.web.pay.service.PayService;
import com.lianniu.web.pay.util.PayConstants;
import com.lianniu.web.util.UUIDUtil;
@Service
public class PayServiceImpl implements PayService{
  private static final int RANDOM_MAX = 10000;
  private static Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);
  @Autowired
  private PayDao payDao;
  @Autowired
  private IBuyerOrderService buyerOrderService;
  @Override
  public BaseResult getPayCode(String prefix) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String timestamp = sdf.format(new Date());
    Random random = new Random();
    String randomInt = String.valueOf(random.nextInt(RANDOM_MAX));
    return BaseResult.success(prefix + timestamp + randomInt);
  }

  @Override
  public BaseResult payRequest(List<String> orderids) {
    logger.info("生成支付请求start："+JSON.toJSONString(orderids));
    List<OrderInfoVo> orders = this.getOrders(orderids);
    PayRequestVo request = insertPayRequest(orders);
    List<PayRequestDetailVo> details = insertPayRequestDetail(request,orders);
    request.setRequestDetails(details);
    return BaseResult.success(request);
  }

  private List<OrderInfoVo> getOrders(List<String> orderids){
    logger.info(String.format("获取[%s]对应的订单列表", JSON.toJSONString(orderids)));
    List<OrderInfoVo> list = new ArrayList<OrderInfoVo>();
    for(String orderid : orderids){
      OrderInfoVo order =(OrderInfoVo) buyerOrderService.getOrderInfo(orderid).getData();
      list.add(order);
    }
    logger.info("获取到的列表为："+JSON.toJSONString(list));
    return list;
  }
  
  private PayRequestVo insertPayRequest(List<OrderInfoVo> orders){
    logger.info("插入生成支付请求");
    String payCode = (String)this.getPayCode("pay").getData();
    PayRequestVo request = new PayRequestVo();
    request.setPay_code(payCode);
    request.setStatus(PayConstants.PAY_REQUEST_STATUS_UNPAY);
    request.setId(UUIDUtil.getUUID());
    logger.info("要插入的支付请求为："+JSON.toJSONString(request));
    payDao.insertPayRequest(request);
    return request;
  }
  
  private List<PayRequestDetailVo> insertPayRequestDetail(PayRequestVo request,List<OrderInfoVo> orders){
    logger.info("插入支付请求明细start");
    List<PayRequestDetailVo> details = new ArrayList<PayRequestDetailVo>();
    for(OrderInfoVo order : orders){
      PayRequestDetailVo detail = new PayRequestDetailVo();
      detail.setId(UUIDUtil.getUUID());
      detail.setOrderid(order.getId());
      detail.setRequestid(request.getId());
      payDao.insertPayRequestDetail(detail);
      details.add(detail);
    }
    logger.info("插入的支付请求明细为:"+JSON.toJSONString(details));
    return details;
  }

  @Override
  public BaseResult paySuccess(PayRequestVo request) {
    boolean updateStatus = payDao.updatePayRequest(request);
    if(!updateStatus){
      return BaseResult.fail(ErrorCode.ExceptionRetCode, "更新支付请求表状态失败！");
    }
    return BaseResult.success();
  }

  @Override
  public BaseResult updateOrderPaySuccess(PayRequestVo request) {
    logger.info("更新订单支付状态start");
    logger.info("更新参数："+JSON.toJSONString(request));
    boolean updateOrderInfo = payDao.updateOrderInfoStatus(request);
    logger.info("更新订单主表状态结果："+updateOrderInfo);
    boolean updateOrderDetail = payDao.updateOrderDetailStatus(request);
    logger.info("更新订单明细表状态："+updateOrderDetail);
    
    if(updateOrderDetail && updateOrderInfo){
      return BaseResult.success();
    }
    
    return BaseResult.fail(ErrorCode.ExceptionRetCode, "更新订单支付状态失败");
  }
}
